/**
 * ajax网络请求工具函数
 * 参数传递形式
 * ajax(  {
            method: 'get',
            url: 'http://10.7.178.117:8088/api/delete',
            data: {
                uid: 101
            },
            success: function (data) {
                console.log('success => ', data);
            },
            error: function (err) {
                console.log('error => ', err);
            }
        })
 * @param {*} options 
 */


//promise封装
function myPromise(options) {
    return new Promise((resolve, reject) => {
        const xhr = new window.XMLHttpRequest()
        let params = formatParmas(options.data) //uid=101
        let method = options.method.toUpperCase()
        if (method == 'GET') {
            xhr.open(method, options.url + '?' + params)
            console.log(options.url + '?' + params)
            xhr.send()
        } else if (method == 'POST') {
            xhr.open(method, options.url)
            xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded')
            xhr.send(params)
        }
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    let data = xhr.responseText
                    data = JSON.parse(data)
                    resolve(data)
                } else {
                    reject('网络请求失败')
                }
            }
        }
    })
}

//ajax封装
function myAjax(options) {
    const xhr = new window.XMLHttpRequest()
    let params = formatParmas(options.data) //uid=101
    let method = options.method.toUpperCase()
    if (method == 'GET') {
        xhr.open(method, options.url + '?' + params)
        xhr.send()
    } else if (method == 'POST') {
        xhr.open(method, options.url)
        xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded')
        xhr.send(params)
    }
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                let data = xhr.responseText
                data = JSON.parse(data)
                options.success(data)
            } else {
                options.error('网络请求失败')
            }
        }
    }
}

//把对象格式转换成键值对
function formatParmas(paramsData) {
    let arr = []
    for (let key in paramsData) {
        arr.push(`${key}=${paramsData[key]}`) //[username=admin,password=123]
    }
    let str = arr.join('&')
    // console.log(str)
    return str
}

//从大到小(数组)
function bubblingArr(arr) {
    for (let i = 0; i < arr.length - 1; i++) {
        for (let j = 0; j < arr.length - 1 - i; j++) {
            if (arr[j] < arr[j + 1]) {
                let temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
    console.log(arr);
}

//从大到小(数组对象)
function bubblingObj(objArr) {
    for (let i = 0; i < objArr.length - 1; i++) {
        for (let j = 0; j < objArr.length - 1 - i; j++) {
            if (objArr[j].focus < objArr[j + 1].focus) {
                let temp = objArr[j];
                objArr[j] = objArr[j + 1];
                objArr[j + 1] = temp;
            }
        }
    }
    // console.log(objArr);
}

//数组对象去重
function clearRepeat(data) {
    bubblingObj(data)
    for (let i = 0; i < data.length; i++) {
        try {
            if (data[i].name == data[i + 1].name) {
                data.splice(i, 1);
                i--;
            }
        } catch (error) {
            break;
        }
    }
    // console.log(data.length, '>>>', data)
}

//户型排序
function typeBubble(arr) {
    //按厅排
    for (let i = 0; i < arr.length - 1; i++) {
        for (let j = 0; j < arr.length - 1 - i; j++) {
            //按‘室’分割成两个数组
            let numArr = arr[j].split('室')
            let numArr2 = arr[j + 1].split('室')
            if (parseInt(numArr[1]) < parseInt(numArr2[1])) {
                let temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
    //按室排
    for (let i = 0; i < arr.length - 1; i++) {
        for (let j = 0; j < arr.length - 1 - i; j++) {
            if (parseInt(arr[j]) < parseInt(arr[j + 1])) {
                let temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
    // console.log(arr);
}

//生成 m 到 n 的随机数
function getRandom(m, n) {
    var max = Math.max(m, n);
    var min = Math.min(m, n);
    var num = Math.floor(Math.random() * (max - min + 1)) + min;
    return num;
}

//保存cookie
function saveCookie(key, value, expires) {
    let date = new Date();
    //中国时区
    // let time = date.getTime() + expires;
    //世界时区  要减8小时
    let time = date.getTime() - 1000 * 60 * 60 * 8 + expires;
    date.setTime(time);
    document.cookie = `${key}=${value};expires=${date}`;
}

//获取cookie
//username=admin; password=123
function getCookie(key) {
    let cookieStr = document.cookie;
    let arr = cookieStr.split(';');
    let value = null;
    arr.forEach(item => {
        let keyValueArr = item.split('=');
        if (key == keyValueArr[0].trim()) {
            value = keyValueArr[1];
        }
    });
    return value;
}

//移除cookie
function removeCookie(key) {
    saveCookie(key, '', -1);
}